package com.jc.softinput;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;

public class HeightChangedLinearLayout extends LinearLayout {

    private static final boolean DEBUG = true;
    private static final String TAG = "ChangedLayout";
    LayoutSizeChangedListener mListener;

    public HeightChangedLinearLayout(Context context) {
        super(context);
}

    public HeightChangedLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HeightChangedLinearLayout(final Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (DEBUG)Log.d(TAG, "onMeasure(): widthMeasureSpec = " + widthMeasureSpec
                + ", heightMeasureSpec = " + heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (DEBUG) Log.d(TAG, "onLayout(): changed = " + changed + ", left = " + l
                + ",top = " + t + ", right = " + r + ", bottom = " + b);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (DEBUG)Log.d(TAG, "onSizeChanged(): oldw = " + oldw + ", oldh = " + oldh
                + ",w = " + w + ", h = " + h);
        final int fw = w;
        final int fh = h;
        final int fow = oldw;
        final int foh = oldh;
        if (mListener != null) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    mListener.onLayoutSizeChanged(fw, fh, fow, foh);
                }
            }).start();
        }
    }

    public void setListener(LayoutSizeChangedListener listener) {
        mListener = listener;
    }

    public interface LayoutSizeChangedListener {
        /**
         * @param w 新的宽度
         * @param h 新的高度
         * @param oldw 旧的宽度
         * @param oldh 旧的高度
         */
        void onLayoutSizeChanged(int w, int h, int oldw, int oldh);
    }
}
